package cn.ecut.controller;


import cn.ecut.model.*;
import cn.ecut.service.GuanliyuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.MessageFormat;
import java.util.*;

@Controller
@RequestMapping("/guanliyuan")
public class GuanliyuanController {

    @Autowired
    private GuanliyuanService guanliyuanService;

    @ResponseBody
    @RequestMapping("/login")
    public Message checkLogin(HttpSession session , String yonghuming, String mima){
        Message message = new Message();
        String s = guanliyuanService.selectMimaByyonghuming(yonghuming);
        if(s.equals(mima)){
           session.setAttribute("yonghuming",yonghuming);
           session.setAttribute("mima",mima);
           message.setFlag(true);
           return message;
        }else {
            message.setFlag(false);
            message.setMsg("用户名或密码错误");
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/changeMima")
    public Message changMima(HttpSession session,String mima,String mima2){
        System.out.println("进入到了controller");
        System.out.println(mima);
        System.out.println(mima2);
        Message message = new Message();
        String yonghuming = (String) session.getAttribute("yonghuming");
        if (mima.equals(session.getAttribute("mima"))){
            Guanliyuan guanliyuan = new Guanliyuan();
            guanliyuan.setMima(mima2);
            guanliyuan.setYonghuming(yonghuming);
            Integer role = guanliyuanService.updateByyonghuming(guanliyuan);
            if(role == 1){
                message.setFlag(true);
                return message;
            }else {
                message.setFlag(false);
                message.setMsg("修改密码失败");
                return message;
            }
        }
        message.setFlag(false);
        message.setMsg("原密码有误");
        return message;
    }

    @ResponseBody
    @RequestMapping("/insertYuangong")
    public Message insertYuangong(HttpSession session ,Yuangong  yuangong){
        Message message = new Message();
        yuangong.setMima("111111");
        int role =guanliyuanService.insertYuangong(yuangong);
        if(role == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            message.setMsg("修改成功");
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/showYuangong")
    public HashMap<String, Object> showYuangong(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map = (HashMap)guanliyuanService.getYuangongMessqge();
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/deleteYuangong")
    public Message deleteYuangong(String yuangongHao){
        Message message = new Message();
        int n = guanliyuanService.deleteYuangongByyangongHao(yuangongHao);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            message.setMsg("删除失败");
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/selectYuangong")
    public HashMap<String, Object> selectYuangong(String searchResult){
        System.out.println("进入到了 /selectYuangong");
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Yuangong> list = new ArrayList<>();
        list = guanliyuanService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/updataYuangong")
    public Message updataYuangong(Yuangong yuangong){
        Message message = new Message();
        String mima=guanliyuanService.selectmimaByyuangongHao(yuangong.getMima());
        yuangong.setMima(mima);
        int role = guanliyuanService.updateAllByYuangongHao(yuangong);
        if (role == 1) {
            message.setFlag(true);
            return message;
        } else {
            message.setFlag(false);
            return message;
        }
    }

}
